<?php

/**
*	A cron function, called by the main hook_cron implementation.
*	Here we first iterate over all the links that were put into
*	a variable called quail_server_links_to_checks, then if we still have
*	time, limited by the admin screen setting on number of pages to 
*	check on a cron run, we then start going over all the exisitng nodes.
*	On existing nodes, we also check links to build up a list of links to
*	check in the future.
*/
function _quail_server_cron() {
  module_load_include('inc', 'quail_server');
  $links_to_check = variable_get('quail_server_links_to_check', array());
  $max = variable_get('quail_server_cron_max', 50);
  $update = 0;
  $last_nid = 0;
  foreach($links_to_check as $link) {
  	$website = node_load($link['website']);
  	$result = drupal_http_request($link['link']);
	if($result->redirect_code == '200' && $result->redirect_url) {
		$result = drupal_http_request($result->redirect_url);
		$link['link'] = $result->redirect_url;
	}
	if($result->data && strpos($result->data, '<html') !== false && ($result->code == '200' || $result->redirect_code == '200') && _quail_server_page_can_be_crawled($website, $link['link'], $result->data)) {
		$links = quail_server_parse_links($link['link'], $result->data, $website->ac_server);
		foreach($links as $link_part) {
			if(!$links_to_check[$link_part] && !quail_server_get_node($link_part)) {
				$links_to_check[$link_part] = array('link' => $link_part, 'website' => $website->nid);
			}
		}
		$url = parse_url($link['link']);
		$html = _quail_server_get_content_from_template($result->data, $website, $link['link']);
		$cms_mode = ($html != $result->data);
		$accessibility = quail_server_check_string($html, $link['link'], 
										$website->ac_server['guideline_nid'], $cms_mode);
		if($accessibility !== false) {
			$node = quail_server_get_node($link['link']);
			if(!property_exists($node, 'content_hash') && $node->content_hash != md5($result->data)) {
				$node->accessibility_results = array('content' => $accessibility);
				$node->website = $website->nid;
				$node->revision = TRUE;
				$node->total_nodes = _quail_server_count_nodes($result->data);
				$node->file_path = $url['path'];
				if($url['query']) {
					$node->file_path .= '?'. $url['query'];
				}
				$node->content_hash = md5($result->data);
				$node->title = _quail_server_get_title($result->data, $url['path']);
				node_save($node);
				$last_nid = $node->nid;
	            $update++;
			}
		}
	}
	$max--;
  }
  //If there's still time, start checking existing nodes
  if($max) {
	  $nodes = db_query('SELECT n.nid, w.url, w.hash, w.website_nid FROM {quail_server_webpage} w LEFT JOIN {node} n ON n.nid = w.nid AND n.vid = w.vid WHERE n.nid > %d  ORDER BY nid LIMIT %d',
	        variable_get('quail_server_last_nid', 0),
	        $max
	      );
	  while($row = db_fetch_object($nodes)) {
	    $last_nid = $row->nid;
	    $website = node_load($row->website_nid);
	    $result = drupal_http_request($row->url);
	    if($result->redirect_code == '200' && $result->redirect_url) {
	    	$result = drupal_http_request($result->redirect_url);
	    	$link['link'] = $row->url;
	    }
	    if($result->data && strpos($result->data, '<html') !== false && ($result->code == '200' || $result->redirect_code == '200') && _quail_server_page_can_be_crawled($website, $row->url, $result->data)) {
	      if(md5($result->data) != $row->hash) {
	        $links = quail_server_parse_links($row->url, $result->data, $website->ac_server);
	        foreach($links as $link) {
	          if(!quail_server_get_node($link)) {
	            $links_to_check[$link] = array('link' => $link, 'website' => $website->nid);
	          }
	        }
	        $url = parse_url($row->url);
			$html = _quail_server_get_content_from_template($result->data, $website, $row->url);
			$cms_mode = ($html != $result->data);
	        $accessibility = quail_server_check_string($html, $row->url, 
	        						$website->ac_server['guideline_nid'], $cms_mode);
	        if($accessibility !== false) {
	          $node = node_load($row->nid);
	          if(!property_exists($node, 'content_hash') && $node->content_hash != md5($result->data)) {
		          $node->accessibility_results = array('content' => $accessibility);
		          $node->website = $context['sandbox']['node']->nid;
		          $node->revision = TRUE;
				  $node->total_nodes = _quail_server_count_nodes($result->data);
		          $node->file_path = $url['path'];
				  if($url['query']) {
				  	$node->file_path .= '?'. $url['query'];
				  }
		          $node->content_hash = md5($result->data);
		          $node->title = _quail_server_get_title($result->data, $url['path']);
		          node_save($node);
		          $update++;
			  }
	        }
	      }
	    }
	  }
  }
  if($last_nid == 0) {
  	 $last_nid = variable_get('quail_server_last_nid', 0) + 
  				 variable_get('quail_server_cron_max', 50);
  }
  $max_nid = db_result(db_query('select max(nid) from {node}'));
  if($last_nid > $max_nid) {
  	$last_nid = 0;
  }
  variable_set('quail_server_last_nid', $last_nid);
  variable_set('quail_server_links_to_check', $links_to_check);
  watchdog('quail_server', t('Checked %number pages for accessibility and updated %updated pages', 
  					  array('%number' => variable_get('quail_server_cron_max', 50), 
  					  '%updated' => $update)));
}

